/*
 * $urlloader v1.0 update from jquery.easyloader 2016.6 by CSS WeidongWang
 */
(function(){
	var modules = {
		tree: {
			js: ['../../cssui/plugins/ztree/js/jquery.ztree.all-3.5.js'],
			css: ['../../cssui/plugins/ztree/css/zTreeStyle/zTreeStyle.css?v=2']
		},
		selectUser: {
			js: [ '../../acl/user/js/selectuser.js']
		},
		category: {
			js: [ '../../disk/js/category.js?v=2'],
			css:[ '../../disk/css/category.css'],
			dependencies:['cloudfile']			
		},
		cloudfile: {
			js: [ '../../disk/js/cloudfile.js?v=3'],
			css:[ '../../disk/css/file.css?v=1'],
			dependencies:['cssuploader','fileTree']			
		},
		fileTree: {
			js: [ '../../disk/js/filetree.js?v=1'],
			dependencies:['tree']			
		},
		usercard: {
			js: [ '../../bbs/jsplugin/usercard/usercard.js?v=1'],
			css:[ '../../bbs/jsplugin/usercard/usercard.css?v=4']
		},
		cssuploader: {
			js: ['../../cssui/plugins/cssuploader/js/cssuploader.js?v=4',
					 '../../cssui/plugins/cssuploader/js/default.js?v=1'],
			css:['../../cssui/plugins/cssuploader/css/default.css?v=2'],
			dependencies:['sparkmd5']			
		},
		kindeditor: {
			js: [ '../../bbs/jsplugin/kindeditor-4.1.2/kindeditor-min.js',
						'../../bbs/jsplugin/kindeditor-4.1.2/lang/zh_CN.js',
						'../../bbs/jsplugin/kindeditor-4.1.2/plugins/code/sh/sh.js'
					],
			css:[ '../../bbs/jsplugin/kindeditor-4.1.2/plugins/code/sh/sh.css']
		},
		ajaxupload: {
			js: [ '../../cssui/plugins/ajaxupload/ajaxupload.3.6.min.js'
			      ,'../../acl/common/upload/js/upload.js']
		},
		sparkmd5: {
			js: [ '../../cssui/plugins/spark-md5/spark-md5.js']
		},
		flowbase:{
			js:['../../flowbase/js/flowbase.js',
			    '../../flowbase/js/getperformer.js'],
			css:['../../flowbase/css/getperformer.css']
		},
		csscard:{
			js:['../../cssui/plugins/csscard/csscard.js'],
			css:['../../cssui/plugins/csscard/csscard.css']
		},
		address: {
			js: [ '../../address/js/addressuser.js',
			      '../../address/js/common_a.js',
			      '../../cssui/plugins/sortable/jquery.sortable.js'
			    ],
			dependencies:['tree','ajaxupload']
		},
		echarts: {
			js: [
			     // '../plugins/echarts-2.2.7/build/source/echarts-all.js'
			     '../plugins/echarts/3.8.5/echarts.js',
			     '../plugins/echarts/3.8.5/jquery.ba-resize.js'
			     ]
			
		}
	};
	var queues = {};
	function loadJs(url, callback){
		var done = false;
		var script = document.createElement('script');
		script.type = 'text/javascript';
		script.language = 'javascript';
		script.src = url;
		script.onload = script.onreadystatechange = function(){
			if (!done && (!script.readyState || script.readyState == 'loaded' || script.readyState == 'complete')){
				done = true;
				script.onload = script.onreadystatechange = null;
				if (callback){
					callback.call(script);
				}
			}
		}
		document.getElementsByTagName("head")[0].appendChild(script);
	}
	function loadCss(url, callback){
		var link = document.createElement('link');
		link.rel = 'stylesheet';
		link.type = 'text/css';
		link.media = 'screen';
		link.href = url;
		document.getElementsByTagName('head')[0].appendChild(link);
		if (callback){
			callback.call(link);
		}
	}
	function loadSingle(urlInfo, callback){
		queues[urlInfo.url] = 'loading';
		var url=(/^http/i.test(urlInfo.url))?urlInfo.url:(urlloader.base + urlInfo.url);
		if(urlInfo.type==1){ //css
			loadCss(url, function(){
				finish();
			});
		}else{ //js
			loadJs(url, function(){
				finish();
			});
		}
		function finish(){
			queues[urlInfo.url] = 'loaded';
			if (callback){
				callback();
			}
		}
	}
	function loadModule(name, callback){
		var urls = [];
		var mm = [];
		if (typeof name == 'string'){
			add(name);
		} else {
			for(var i=0; i< name.length; i++){
				add(name[i]);
			}
		}
		var tmpUrls = ['js','css'];
		for(var i=0; i< mm.length; i++){
			tmpUrls[0]  = _.union( tmpUrls[0], modules[mm[i]]['js']);
			tmpUrls[1]  = _.union( tmpUrls[1], modules[mm[i]]['css']);
		}
		var k=0;
		for(var i=0;i<2;i++)
			for(var j=0;j<tmpUrls[i].length;j++)
				urls[k++]={url:tmpUrls[i][j], type: i};
			
		function add(name){
			if (!modules[name]) return;
			var d = modules[name]['dependencies'];
			if (d){
				for(var i=0; i<d.length; i++){
					add(d[i]);
				}
			}
			mm.push(name);
		}	
		function finish(){
			if (callback){
				callback();
			}
		}
		var time = 0;
		function loadUrls(){
			if (urls.length){
				var urlInfo = urls[0];
				if (!queues[urlInfo.url]){
					loadSingle(urlInfo,function(){
						urls.shift();
						loadUrls();
					});
				} else if (queues[urlInfo.url] == 'loaded'){
						urls.shift();
						loadUrls();
				} else {
					if (time < urlloader.timeout){
						time += 10;
						setTimeout(arguments.callee, 10);
					}
				}
			} else {
					finish();
			}
		}	
		loadUrls();
	}
	urlloader = {
		modules:modules,
		base:'.',
		timeout:2000,
		load: function(names, callback){
			loadModule(names, callback);
		}
	};
	var scripts = document.getElementsByTagName('script');
	for(var i=0; i<scripts.length; i++){
		var src = scripts[i].src;
		if (!src) continue;
		var m = src.match(/urlloader\.js(\W|$)/i);
		if (m){
			urlloader.base = src.substring(0, m.index);
		}
	}
	window.using = urlloader.load;
})();